<template>
  <a-row align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item :style="{ padding: 0, marginRight: '38px' }" disabled>
          <div class="title-bar">
            <img class="logo" src="../assets/qihang.png" />
            <div class="title">LongFa在线评测</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in showRouter" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>{{ userStore.state.user?.loginUser?.userAccount ?? "未登录" }}</div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import { routes } from "@/router/routes";

const userStore = useStore();

const router = useRouter();
//展示在菜单的路由
const showRouter = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      //返回为 false的元素从数组中移除
      return false;
    }
    // 根据权限显示菜单
    if (
      !checkAccess(
        userStore.state.user?.loginUser,
        item?.meta?.access as string
      )
    ) {
      return false;
    }
    //返回true的元素保留在数组
    return true;
  });
});
// 默认主页
const selectedKeys = ref(["/"]);

//路由跳转时，更新选中的菜单项
router.afterEach((to, form, failure) => {
  selectedKeys.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title-bar .logo {
  height: 48px;
  align-items: center;
}

.title {
  color: #444;
}
</style>
